package com.reggie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.reggie.dto.OrdersDto;
import com.reggie.mapper.OrderMapper;
import com.reggie.pojo.Category;
import com.reggie.pojo.Orders;
import com.reggie.service.OrderService;
import com.reggie.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Override
    public PageUtil<Orders> findAll(Integer page, Integer pageSize, String number, String beginTime, String endTime) {
        PageUtil<Orders> orderPageUtil = new PageUtil<>();
        PageHelper.startPage(page, pageSize);

        List<Orders> orderList = orderMapper.findAll(page, pageSize, number, beginTime, endTime);
        if (orderList==null) return null;

        Page<Orders> orderPage = (Page<Orders>) orderList;
        orderPageUtil.setTotal(orderPage.getTotal());
        orderPageUtil.setRecords(orderPage.getResult());
        return orderPageUtil;
    }

    @Override
    public boolean editStatus(Integer status, Long id) {
        if (orderMapper.editStatus(status, id)) return true;
        return false;
    }

    @Override
    public boolean add(Orders orders) {
        if (orderMapper.add(orders)==1) return true;
        return false;
    }

    @Override
    public List<OrdersDto> findByUserId(Integer page, Integer pageSize, Long userId) {
        List<OrdersDto> ordersDtoList = orderMapper.findByUserId(page, pageSize, userId);
        return ordersDtoList;
    }

    @Override
    public Orders findById(Long id) {
        Orders orders = orderMapper.find(id);
        return orders;
    }
}
